package com.wxtwxt.ruralmall.coupon.service.impl;

import com.wxtwxt.ruralmall.common.to.MemberPrice;
import com.wxtwxt.ruralmall.common.to.SkuReductionTo;
import com.wxtwxt.ruralmall.coupon.entity.MemberPriceEntity;
import com.wxtwxt.ruralmall.coupon.entity.SkuLadderEntity;
import com.wxtwxt.ruralmall.coupon.service.MemberPriceService;
import com.wxtwxt.ruralmall.coupon.service.SkuLadderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxtwxt.ruralmall.common.utils.PageUtils;
import com.wxtwxt.ruralmall.common.utils.Query;

import com.wxtwxt.ruralmall.coupon.dao.SkuFullReductionDao;
import com.wxtwxt.ruralmall.coupon.entity.SkuFullReductionEntity;
import com.wxtwxt.ruralmall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveReductionInfo(SkuReductionTo skuReductionTo) {
        Long skuId = skuReductionTo.getSkuId();
        if(skuReductionTo.getFullCount() > 0 || skuReductionTo.getDiscount().compareTo(new BigDecimal(0)) > 0) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuId);
            skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
            skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }
        if(skuReductionTo.getFullPrice().compareTo(new BigDecimal(0)) > 0 || skuReductionTo.getReducePrice().compareTo(new BigDecimal(0)) > 0) {
            SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
            skuFullReduction.setSkuId(skuId);
            skuFullReduction.setFullPrice(skuReductionTo.getFullPrice());
            skuFullReduction.setReducePrice(skuReductionTo.getReducePrice());
            skuFullReduction.setAddOther(skuReductionTo.getPriceStatus());
            this.save(skuFullReduction);
        }
        List<MemberPrice> memberPriceList = skuReductionTo.getMemberPrice();
        if(memberPriceList.size() > 0) {
            List<MemberPriceEntity> memberPriceEntities = memberPriceList.stream().map(memberPrice -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuId);
                memberPriceEntity.setMemberLevelId(memberPrice.getId());
                memberPriceEntity.setMemberLevelName(memberPrice.getName());
                memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).filter(memberPriceEntity -> memberPriceEntity.getMemberPrice().compareTo(new BigDecimal(0)) > 0)
              .collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }

}